package com.hmall.api.config;

import com.hmall.api.fallback.*;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {
    //注册feign日志记录级别：none->basic->headers->full
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor feignRequestInterceptor(){
        return  new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                if(userId!=null){
                    requestTemplate.header("user-info",userId.toString());
                }
            }
        };
    }
    //注册关于商品远程调用客户端的fallback
    @Bean
    public ItemClientFallback itemClientFallback(){
        return  new ItemClientFallback();
    }
    @Bean
    public CartClientFallback cartClientFallback(){
        return  new CartClientFallback();
    }
    @Bean
    public TradeClientFallback tradeClientFallback(){
        return  new TradeClientFallback();
    }
    @Bean
    public UserClientFallback userClientFallback(){
        return  new UserClientFallback();
    }

    @Bean
    public PayClientFalllback payClientFalllback(){
        return new PayClientFalllback();
    }
}
